package com.ych.modules.cms.authorInfc.user.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.base.dbEntity.TUserInfoEntity;
import com.ych.modules.common.entity.modify.ModifyUserPwdEntity;
import com.ych.modules.common.entity.query.QueryUserInfoEntity;
import com.ych.modules.common.service.UserInfoService;
import com.ych.redis.RedisUtil;
import com.ych.redis.config.RedisKeyConfig;
import com.ych.utils.TLMap;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/31
 * Time: 14:25
 * Description:
 */
@RestController("cmsUserController")
@RequestMapping("/cms/auth/user")
@Api(tags = {"CMS——用户相关"}, description = "CMS——用户相关", position = 2)
public class UserController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisUtil       redisUtil;

    @RequiresPermissions("cms:auth:user")
    @PostMapping("/listUserInfo")
    @ApiOperation(value = "用户列表查询", notes = "用户列表查询")
    public R<Page<TUserInfoEntity>> listUserInfo(@RequestBody QueryUserInfoEntity entity) {
        try {
            Page<TUserInfoEntity> page = userInfoService.listUserInfo(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/getUserInfo")
    @ApiOperation(value = "查询用户详情信息", notes = "查询用户详情信息")
    public R<TUserInfoEntity> getUserInfo(@RequestBody String userId) {
        try {
            TUserInfoEntity userInfoEntity = userInfoService.getUserInfo(userId);
            return new R(0, "成功", userInfoEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/modifyPwd")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public R<String> modifyPwd(@RequestBody ModifyUserPwdEntity entity) {
        try {
            userInfoService.modifyPwd(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:user-operation")
    @PostMapping("/resetPwd")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    public R<String> resetPwd(@RequestBody String userId) {
        try {
            userInfoService.resetPwd(userId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @RequiresPermissions("cms:auth:user-operation")
    @ApiOperation(value = "修改用户 userId,status,userLabelTag", position = 7)
    @PostMapping("/userInfo/update")
    public R<String> update(@RequestBody TUserInfoEntity user) {
        try {
            if (StringUtils.isBlank(user.getUserId())) {
                throw new RuntimeException("用户ID不能为空！");
            }
            userInfoService.cmsUpdate(user);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
        return new R(0, "修改成功", null);
    }

    @ApiOperation(value = "退出登录", position = 5)
    @PostMapping("/logout")
    public R<String> logout() {
        String userId = TLMap.getUserId();
        if (userId == null || "".equals(userId)) {
            return new R(1, "传入用户id不可为空！", null);
        }
        try {
            userInfoService.logout(userId);
        } catch (Exception e) {

        }
        try {
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {

        }
        String sessionId = RedisKeyConfig.sessionToken(TLMap.getUserId());
        redisUtil.redisTemplate().delete(sessionId);
        return new R(0, "注销成功", null);
    }

    @ApiOperation(value = "根据用户id，查询用户信息")
    @PostMapping("/queryByUserId")
    public R<TUserInfoEntity> queryByUserId(@RequestBody String userId) {
        try {
            if (StringUtils.isBlank(userId)) {
                return new R(1, "传入用户id不可为空", null);
            }
            TUserInfoEntity userInfoEntity = userInfoService.queryByUserId(userId);
            return new R(0, "成功", userInfoEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
